package com.base.cn.platform.os.controller.agent.user;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.entity.agent.user.AgentUser;
import com.base.cn.platform.os.service.course.agent.user.AgentUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台推广员Controller
 *
 * @author LIZHIPENG
 * @since 2018/5/7
 */
@Controller
@RequestMapping("/admin/agent/user/")
public class AdminAgentUserController extends AdminBaseController {

    @Autowired
    private AgentUserService agentUserService;

    /**
     * 初始化推广员列表页面
     * @return
     */
    @RequestMapping("/queryAgentUserList")
    public String queryAgentUserList(){
        return resultPage("/agent/user/agent-user-list");
    }

    /**
     * 初始化选择一级推广员列表页面
     * @return
     */
    @RequestMapping("/selectAgentUserList")
    public String selectAgentUserList(){
        return resultPage("/agent/user/select-agent-user-list");
    }

    /**
     * 分页查询推广员列表
     * @param model
     * @param currentPage 页码
     * @param params 条件
     * @return
     */
    @RequestMapping("/ajaxQueryAgentUserList")
    public String ajaxQueryAgentUserList(Model model,
                                         @RequestParam(value = "currentPage", required = false, defaultValue = "1")int currentPage,
                                         @RequestBody Map<String, Object> params){
        String flag = (String)params.get("flag");
        //查询标识：1.选择一级推广员，2.查询下级推广员列表，3.查询推广员列表
        Integer _flag;
        if(flag == null){
            _flag = 0;
        }else {
            _flag = Integer.valueOf(flag);
        }

        int pageSize = 10;
        if(_flag == 2){
            pageSize = 1;
        }
        String typeTo = (String)params.get("typeTo");
        Map<String, Object> dataMap = agentUserService.queryAgentUserListPage(currentPage, pageSize, params,true, true,true, true);
        model.addAttribute("dataMap", dataMap);
        model.addAttribute("typeTo", typeTo);

        if(_flag == 1){
            return resultPage("/agent/user/ajax-select-agent-user-list");
        }else if(_flag == 2) {
            return resultPage("/agent/user/ajax-sun-agent-user-list");
        }else{
            return resultPage("/agent/user/ajax-agent-user-list");
        }
    }

    /**
     * 初始化添加，修改页面
     * @param model
     * @param agentUserId 推广员ID
     * @return
     */
    @RequestMapping(value = {"/initAdd1", "/initUpdate1","/initAdd2", "/initUpdate2"})
    public String initAddOrUpdateView(Model model,
                                      @RequestParam(value = "agentUserId", required = false,defaultValue = "0")BigDecimal agentUserId,
                                      @RequestParam("userType")int userType){
        AgentUser agentUser = null;
        if(DataUtil.idIsNotNull(agentUserId)){
            agentUser = agentUserService.findAgentUserById(agentUserId,true,false,false,false);
        }
        if(agentUser == null){
            agentUser = new AgentUser();
        }
        model.addAttribute("agentUser", agentUser);
        model.addAttribute("userType", userType);
        return resultPage("/agent/user/save-agent-user");
    }

    /**
     * 保存推广员方法
     * @param request
     * @param agentUser
     * @return
     */
    @RequestMapping("/saveOrUpdateAgentUser")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "新建推广员或者修改")
    public Map<String, Object> saveOrUpdateAgentUser(HttpServletRequest request,
                                                     @RequestBody Map<String, Object> agentUser){
        BigDecimal createUserId = loginUtil.getLoginUserId(request);
        agentUser.put("createFrom", 1);
        agentUser.put("createUserId", createUserId);
        return agentUserService.saveAgentUser(agentUser);
    }

    /**
     * 查看推广员详情
     * @param model
     * @param agentUserId 推广员ID
     * @return 页面
     */
    @RequestMapping("/agentUserInfo")
    public String agentUserInfo(Model model,
                                @RequestParam("agentUserId") BigDecimal agentUserId){
        AgentUser agentUser = agentUserService.findAgentUserById(agentUserId,true,true,true,true);
        model.addAttribute("agentUser", agentUser);
        return resultPage("/agent/user/agent-user-info");
    }

    /**
     * 修改状态
     * @param agentUserIds
     * @param status
     * @return
     */
    @RequestMapping("/updateStauts")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改推广员状态")
    public Map<String, Object> updateStauts(@RequestParam("agentUserIds") String agentUserIds,
                                            @RequestParam("status")int status){
        return agentUserService.updateAgentUserStatus(agentUserIds, status);
    }

    /**
     * 删除推广员
     * @param agentUserId 推广员Id
     * @return
     */
    @RequestMapping("/deleteAgentUser")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "删除推广员")
    public Map<String, Object> deleteAgentUser(@RequestParam("agentUserId")BigDecimal agentUserId){
        return agentUserService.deleteAgentUser(agentUserId);
    }

    /**
     * 去往审核页面
     * @param model
     * @param agentUserId
     * @return
     */
    @RequestMapping("/initAduit")
    public String initAduit(Model model,
                            @RequestParam("agentUserId")BigDecimal agentUserId){
        AgentUser agentUser = agentUserService.findAgentUserById(agentUserId,true,true,true,true);
        model.addAttribute("agentUser", agentUser);
        return resultPage("/agent/user/aduit-agent-user");
    }

    /**
     * 审核推广员
     * @param agentUserId 推广员ID
     * @param flag 审核标识：true：通过，false：驳回
     * @param rejectedCause 驳回原因
     * @return Map<String, Object>
     */
    @RequestMapping("/auditAgentUser")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "审核推广员")
    public Map<String, Object> auditAgentUser(@RequestParam("agentUserId") BigDecimal agentUserId,
                                              @RequestParam("flag") boolean flag,
                                              @RequestParam(value = "rejectedCause", required = false) String rejectedCause){
        return agentUserService.aduitAgentUser(agentUserId, flag, rejectedCause);
    }

    /**
     * 初始化导入推广员页面
     * @return String
     */
    @RequestMapping("/initImportAgentUserView")
    public String initImportAgentUserView(){
        return resultPage("/agent/user/import-agent-user");
    }

    /**
     * 上传推广员
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadAgentUser", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "上传推广员")
    public Map<String, Object> uploadAgentUser(@RequestPart(value = "file") MultipartFile file){
        return agentUserService.uploadAgentUser(file);
    }

    /**
     * 推广员导出
     * @param agentUserName
     * @param mobile
     * @param userType
     * @param createFrom
     * @param status
     * @param beginTime
     * @param endTime
     * @return
     * @throws Exception
     */
    @RequestMapping("/downloadExcelAgentList")
    public ResponseEntity<byte[]> downloadExcelAgentList(@RequestParam(value = "agentUserName",required = false) String agentUserName,
                                                         @RequestParam(value = "mobile",required = false) String mobile,
                                                         @RequestParam(value = "userType",required = false) Integer userType,
                                                         @RequestParam(value = "createFrom",required = false) Integer createFrom,
                                                         @RequestParam(value = "status",required = false) Integer status,
                                                         @RequestParam(value = "beginTime",required = false) String beginTime,
                                                         @RequestParam(value = "endTime",required = false) String endTime) throws Exception{
        Map<String,Object> map = new HashMap<>();
        map.put("agentUserName",agentUserName);
        map.put("mobile",mobile);
        map.put("userType",userType);
        map.put("createFrom",createFrom);
        map.put("beginTime",beginTime);
        map.put("endTime",endTime);
        map.put("status",status);
        return agentUserService.downloadExcelAgentList(map,2000);
    }


    /**
     * 修改平台所有推广员的结算周期
     * @return
     */
    @RequestMapping("/alterAgentSettType")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.OTHER, action = "修改平台所有推广员的结算周期")
    public Map<String, Object> alterAgentSettType(@RequestParam("settType") int  settType,@RequestParam("ids") String  ids){
        return agentUserService.alterAgentSettType(settType,ids);
    }


    /**
     * 选择推广员页面
     * @return
     */
    @RequestMapping("/openSelectAgentUserList")
    public String openSelectAgentUserList(){
        return resultPage("/agent/user/agent-open-user-list");
    }

}
